﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_MasFieldPositions : System.Web.UI.UserControl
    {
        private IFieldBLL fieldBLL = new FieldBLL();
        private IEmployeeTypeBLL emptypeBLL = new EmployeeTypeBLL();
        private IPositionTypeBLL postypeBLL = new PositionTypeBLL();
        private IFieldGroupBLL groupBLL = new FieldGroupBLL();
        private IClassSpecificationBLL specBLL = new ClassSpecificationBLL();
        
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                FillDataToEmpType(); // Combobox ประเภทบุคลากร
                 // Combobox กลุ่มอาชีพ
                // FillDataToRecordStatus();

                if (DataItem != null && !(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    string employeeTypeSeq = Convert.ToString(DataBinder.Eval(DataItem, "EmployeeTypes.Sequence"));
                    txtFieldName.Text = DataBinder.Eval(DataItem, "Name").ToString();
                    txtFieldNameEng.Text = DataBinder.Eval(DataItem, "NameEng").ToString();
                    txtFieldLine.Text = DataBinder.Eval(DataItem, "LineName").ToString();
                    txtFieldLineEng.Text = DataBinder.Eval(DataItem, "LineNameEng").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                    lblSeq.Text = Convert.ToString(DataBinder.Eval(DataItem, "Sequence"));
                    btnUpdate.Visible = false;
                    btnAddnew.Visible = true;

                    if (cmbEmpType.FindItemByValue(employeeTypeSeq) != null)
                    {
                        cmbEmpType.SelectedValue = employeeTypeSeq;
                        FillDataToPositionType(Int32.Parse(cmbEmpType.SelectedValue)); // Combobox ตำแหน่งประเภท
                    }

                    if (DataBinder.Eval(DataItem, "PositionTypes.Sequence") != null)
                    { 
                        string positionTypesSeq = Convert.ToString(DataBinder.Eval(DataItem, "PositionTypes.Sequence"));

                        if (cmbPosType.FindItemByValue(positionTypesSeq) != null)
                        {
                            cmbPosType.SelectedValue = positionTypesSeq;
                            FillDataToClassSpec(Int32.Parse(cmbPosType.SelectedValue)); // Combobox ประเภทตำแหน่ง
                        } 
                    }

                    FillDataToFieldGroup(Int32.Parse(employeeTypeSeq));

                    if (DataBinder.Eval(DataItem, "FieldGroups.Sequence") != null)
                    {
                        string fieldGroupSeq = Convert.ToString(DataBinder.Eval(DataItem, "FieldGroups.Sequence"));

                        if (cmbFieldGroup.FindItemByValue(fieldGroupSeq) != null)
                        {
                            cmbFieldGroup.SelectedValue = fieldGroupSeq; // Combobox กลุ่มอาชีพ
                        } 
                    }

                    if (DataBinder.Eval(DataItem, "ClassSpecifications.Sequence") != null)
                    { 
                        string classSpecSeq = Convert.ToString(DataBinder.Eval(DataItem, "ClassSpecifications.Sequence"));

                        if (cmbClassSpec.FindItemByValue(classSpecSeq) != null)
                        {
                            cmbClassSpec.SelectedValue = classSpecSeq; // Combobox ประเภทตำแหน่ง
                        } 
                    }

                }
            }
            catch (Exception err)
            {
                throw err;
            }
        }

        public object DataItem
        {
            get { return this._dataItem; }
            set { this._dataItem = value; }
        }

        public void FillDataToEmpType()
        {
            try
            {
                if (cmbEmpType.Items.Count == 0)
                {
                    IList<EmployeeTypeInfo> InfoList = emptypeBLL.ListEmployeeTypes(null);
                    cmbEmpType.DataSource = InfoList;

                    if (cmbEmpType.Items.Count > 0)
                    {
                        cmbEmpType.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception Err)
            {
                throw Err;
            }
        }

        public void FillDataToPositionType(int EmpType)
        {
            try
            {
                if (cmbPosType.Items.Count == 0)
                {
                    IList<PositionTypeInfo> InfoList = postypeBLL.ListPositionTypes(null, EmpType);
                    cmbPosType.DataSource = InfoList;
                    cmbPosType.DataBind();
                }
            }
            catch (Exception Err)
            {

            }
        }

        public void FillDataToFieldGroup(int EmpType)
        {
            try
            {
                if (cmbFieldGroup.Items.Count == 0)
                {
                    IList<FieldGroupInfo> InfoList = groupBLL.ListFieldGroups(null, EmpType);
                    List<FieldGroupInfo> list = new List<FieldGroupInfo>();

                    for (int i = 0; i < InfoList.Count; i++)
                    {
                        FieldGroupInfo c = new FieldGroupInfo();
                        c.Sequence = InfoList[i].Sequence;
                        c.Name = InfoList[i].Name + InfoList[i].Description;

                        list.Add(c);
                    }
                    cmbFieldGroup.DataSource = list;
                    cmbFieldGroup.DataBind();
                }
            }
            catch (Exception Err)
            {

            }
        }

        public void FillDataToClassSpec(int PosTypeSeq)
        {
            try
            {
                if (cmbClassSpec.Items.Count == 0)
                {
                    IList<ClassSpecificationInfo> InfoList = specBLL.ListClassSpecification(null, PosTypeSeq);

                    cmbClassSpec.DataSource = InfoList;
                    cmbClassSpec.DataBind();
                }
            }
            catch (Exception Err)
            {

            }
        }

        public void FillDataToRecordStatus()
        {
            //I_MAS_RecordStatuses mas = new B_MAS_RecordStatuses();
            //DataSet ds = mas.LoadListRecStatusForCmd();

            //cmbStatus.DataTextField = "RECORD_STATUS_NAME";
            //cmbStatus.DataValueField = "RECORD_STATUS";
            //cmbStatus.DataSource = ds;

        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            int empType, posType, fieldGroup, classSpec;
            Int32.TryParse(cmbEmpType.SelectedValue, out empType);
            Int32.TryParse(cmbPosType.SelectedValue, out posType);
            Int32.TryParse(cmbFieldGroup.SelectedValue, out fieldGroup);
            Int32.TryParse(cmbClassSpec.SelectedValue, out classSpec);
            string a = SsMng.SsUserType.EmpSeq.ToString();

            fieldBLL.Insert(txtFieldName.Text.Trim()
                            , txtFieldNameEng.Text.Trim()
                            , txtFieldLine.Text.Trim()
                            , txtFieldLineEng.Text.Trim()
                            , txtRemarks.Text.Trim()
                            , empType
                            , posType
                            , fieldGroup
                            , classSpec //(int)(int?)null 
                            , 1);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int empType, posType, fieldGroup, classSpec;
            Int32.TryParse(cmbEmpType.SelectedValue, out empType);
            Int32.TryParse(cmbPosType.SelectedValue, out posType);
            Int32.TryParse(cmbFieldGroup.SelectedValue, out fieldGroup);
            Int32.TryParse(cmbClassSpec.SelectedValue, out classSpec);

            fieldBLL.Update(Int32.Parse(lblSeq.Text.Trim())
                            , txtFieldName.Text.Trim()
                            , txtFieldNameEng.Text.Trim()
                            , txtFieldLine.Text.Trim()
                            , txtFieldLineEng.Text.Trim()
                            , txtRemarks.Text.Trim()
                            , empType
                            , posType
                            , fieldGroup
                            , classSpec
                            , 1);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbPosType_SelectedIndexChanged(object o,
                                                       Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            try
            {
                int nPosTypeSeq =
                    Int32.Parse(string.IsNullOrEmpty(cmbPosType.SelectedValue) ? null : cmbPosType.SelectedValue);

                cmbClassSpec.Items.Clear();
                FillDataToClassSpec(nPosTypeSeq);
            }
            catch (Exception Err)
            {
                throw Err;
            }
        }

        protected void cmbEmpType_SelectedIndexChanged(object o,
                                                       Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            try
            {
                int EmpType =
                    Int32.Parse(string.IsNullOrEmpty(cmbEmpType.SelectedValue) ? null : cmbEmpType.SelectedValue);

                cmbPosType.Items.Clear();
                cmbFieldGroup.Items.Clear();
                FillDataToPositionType(EmpType);
                FillDataToFieldGroup(EmpType);
            }
            catch (Exception Err)
            {
                throw Err;
            }
        } 
    }
}